﻿using System;
using System.Collections.Generic;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// DefectInventory操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DefectInventorysController : ControllerBase
    {
        private readonly DefectInventoryApp _app;

        //获取详情
        [HttpGet]
        public Response<DefectInventory> Get(string id)
        {
            var result = new Response<DefectInventory>();
            try
            {
                result.Result = _app.Get(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        //添加
        [HttpPost]
        public Response Add(DefectInventory obj)
        {
            var result = new Response();
            try
            {
                _app.Add(obj);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        //修改
        [HttpPost]
        public Response Update(DefectInventory obj)
        {
            var result = new Response();
            try
            {
                _app.Update(obj);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 获取隐患分类列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response GetDefectCategoryList()
        {
            var result = new Response<List<Category>>();
            try
            {
                result.Result = _app.GetDefectCategoryList();

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 根据部门获取设备列表
        /// </summary>
        /// <param name="orgId"></param>
        /// <returns></returns>
        [HttpGet]
        public Response GetEquipList(string orgId)
        {
            var result = new Response<List<EquipmentInventory>>();
            try
            {
                result.Result = _app.GetEquipList(orgId);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 加载列表
        /// </summary>
        [HttpGet]
        public TableData Load([FromQuery]QueryDefectInventoryListReq request)
        {
            return _app.Load(request);
        }

        /// <summary>
        /// 获取某隐患详情信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public Response GetDetail(string id)
        {
            var result = new Response<DefectInventoryView>();
            try
            {
                result.Result = _app.GetDetail(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 获取某隐患附件
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        [HttpGet]
        public Response GetFileByDefectGuid(string guid)
        {
            var result = new Response<List<UploadFile>>();
            try
            {
                result.Result = _app.GetUploadFiles(guid);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        [HttpPost]
        public Response Delete([FromBody]string[] ids)
        {
            var result = new Response();
            try
            {
                _app.Delete(ids);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        public DefectInventorysController(DefectInventoryApp app)
        {
            _app = app;
        }
    }
}
